/**
 *
 */
package com.bsoft.gol.hbs.dao;

import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import ctd.spring.boot.annotation.SsdevDao;
import ctd.util.annotation.RpcService;
import hc.message.entity.MsgNotifyConfig;
import hc.message.entity.qo.MsgNotifyQo;
import java.math.BigInteger;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.StatelessSession;

/**
 * @since 2018-03-05 01:58:26
 */
@SsdevDao(serviceId = "msgNotifyConfigService")
public abstract class MsgNotifyConfigDAO extends ctd.persistence.support.hibernate.HibernateSupportDelegateDAO<MsgNotifyConfig> {

    public MsgNotifyConfigDAO() {
        this.setKeyField("cfgId");
    }

    @RpcService
    @DAOMethod(sql = "from MsgNotifyConfig where cfgId=:cfgId")
    public abstract MsgNotifyConfig getMsgNotifyConfigById(@DAOParam("cfgId") String cfgId);

    @RpcService
    @DAOMethod(sql = "from MsgNotifyConfig")
    public abstract List<MsgNotifyConfig> findMsgNotifyConfigs();

    @RpcService
    @DAOMethod(sql = "from MsgNotifyConfig where cfgCode = :cfgCode and status = 0")
    public abstract MsgNotifyConfig getConfigByCfgCode(@DAOParam("cfgCode") String cfgCode);

    @RpcService
    @DAOMethod(sql = "update MsgNotifyConfig set status = :status where cfgId = :cfgId")
    public abstract void updateNotifyConfigStatus(@DAOParam("status") byte status, @DAOParam("cfgId") Integer cfgId);

    @RpcService
    public List<MsgNotifyConfig> findMsgNotifyConfigByPage(MsgNotifyQo qo) {
        HibernateStatelessResultAction<List<MsgNotifyConfig>> action = new AbstractHibernateStatelessResultAction<List<MsgNotifyConfig>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuffer hsql = new StringBuffer();
                hsql.append(" ");
                getSql(hsql, qo);
                Query sqlQuery = ss.createQuery(hsql.toString());
                sqlQuery.setFirstResult(qo.getStartRow());
                sqlQuery.setMaxResults(qo.getPageSize());
                List<MsgNotifyConfig> dbList = sqlQuery.list();
                setResult(dbList);
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    @RpcService
    public long getMsgNotifyConfigByPageCount(MsgNotifyQo qo) {
        HibernateStatelessResultAction<Long> action = new AbstractHibernateStatelessResultAction<Long>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuffer hsql = new StringBuffer();
                hsql.append("SELECT count(1) ");
                getSql(hsql, qo);
                Query query = ss.createSQLQuery(hsql.toString());

                List list = query.list();
                if (list.size() == 1) {
                    Object o = list.get(0);
                    BigInteger count = (BigInteger) o;
                    setResult(count.longValue());
                }
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    private void getSql(StringBuffer hsql, MsgNotifyQo qo) {
        hsql.append("from msg_notify_config ");
        if (qo.getStatus() != null) {
            hsql.append("where status = ").append(qo.getStatus()).append(" ");
        }
    }

    @RpcService
    public void saveMsgNotifyConfig(MsgNotifyConfig msgNotifyConfig) {
        this.save(msgNotifyConfig);
    }

    @RpcService
    public void updateMsgNotifyConfig(MsgNotifyConfig msgNotifyConfig) {
        this.update(msgNotifyConfig);
    }

    @RpcService
    public void deleteMsgNotifyConfig(Integer msgNotifyConfigId) {
        this.remove(msgNotifyConfigId);
    }
}